import java.util.Arrays;

public class MyStack {
    private int[] elem; //存放数据的数组
    private int size; //有效元素个数
    private static final int DEFAULT_CAPACITY = 10; //约定好的默认容量

    public MyStack() {
        this.elem = new int[DEFAULT_CAPACITY];
        this.size = 0;
    }

    // 压栈
    public int push(int data) {
        // 判断是否需要增容
        if (this.size == this.elem.length) {
            this.elem = Arrays.copyOf(this.elem, this.size * 2);
        }
        // 压栈只能往栈顶压栈
        this.elem[this.size++] = data;
        return data;
    }

    // 出栈
    public int pop() {
        // 判断栈是否为null
        if (this.size == 0) {
            throw new MyStackEmptyException("栈为空！");
        }

        return this.elem[--this.size];
    }

    // 查看栈顶元素
    public int peek() {
        // 判断栈是否为null
        if (this.size == 0) {
            throw new MyStackEmptyException("栈为空！");
        }
        return this.elem[this.size - 1];
    }

    public boolean empty() {
        return this.size == 0;
    }

}
